package com.example.administrator.testc.WebViewCacahe;/**
 * Created by Administrator on 2016/5/29 0029.
 */

import android.util.Log;
import android.webkit.WebView;

import com.example.administrator.testc.R;
import com.example.administrator.testc.mApp;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import pri.zey.custom.tools.Tkit;

/**
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>类描述
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>属性描述
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>方法描述
 */
public class TloadUrl {

    private static final String webCachePath = mApp.getContext().getFilesDir().getAbsolutePath() + "webCachePath";
    private final String domain = "http://m.xincheping.com/";
    private final String sbuStr = "/data/user/0";
    private final String subImg = "src=\"";

    private String url;

    public TloadUrl() {
        File file = new File(webCachePath);
        if(!file.exists()){
            file.mkdirs();
        }
    }

    public void init(final String url, final WebView webView){
        String loadUlr = url;
        final String filePath = webCachePath;
        final String fileName = UrlGetFileName(url);
        File file = isCheckFile(filePath + "/" + fileName);
        if (file == null) {
            webView.loadUrl(loadUlr);
            new MyHttpClient().setRemoteUrl(url).setiHttpStr(new MyHttpClient.IHttpStr() {
                @Override
                public void onResult(final String jsonStr){
                    String[] conditionArr = new String[]{"(\"|')\\/.*\\.css", "(src|url=\").*?(jpg|gif)\"", "src=\".*\\.js\\\""};
                    for (String conditionStr : conditionArr){
                        String lurl = "";
                        for (String str : hasStrArr(jsonStr, conditionStr)) {
                            if(str.contains("css"))
                                str.substring(1);
                            if(str.contains(subImg))
                                str.substring(subImg.length(), str.length() - 1);
                            if (str.startsWith("http://"))
                                lurl = domain + str;
                            else
                                lurl = str;
                            Log.i("TAGSS", "str : " + lurl);
                        }
                    }

                    webView.post(new Runnable() {
                        @Override
                        public void run() {
                            mSaveFile(jsonStr, filePath + "/" + fileName);
                        }
                    });
                }
            }).start();
        } else {
            loadUlr = "file://" + (filePath + "/" + fileName);
            webView.loadUrl(loadUlr);
        }

    }

    private String UrlGetFileName(String url){
        String fileName = "";
        if(Tkit.notBlank(url)){
            String[] strArr = url.split("\\/");
            fileName = strArr[strArr.length - 1];
            String[] rsArr = mApp.getContext().getResources().getStringArray(R.array.addHttp_url);
            for (String str : rsArr){
                if(hasStr(url, str)){
                    fileName = fileName + ".html";
                }
            }
            return fileName;
        } else {
            return fileName;
        }
    }

    private File isCheckFile(String filePath){
        File file = new File(filePath);
        if(file.exists()){
            return file;
        } else{
            return null;
        }
    }

    private boolean mSaveFile(String contentStr, String filePath){
        boolean isSave = false;
        if(Tkit.notBlank(contentStr)){
            File file = new File(filePath);
            if(!file.exists()){
                try {
                    PrintWriter printWriter = new PrintWriter(new FileOutputStream(file));
                    printWriter.print(contentStr);
                    printWriter.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                isSave = false;
            } else {
                isSave = true;
            }
        } else {
            Log.e("TAGSS", "****contentStr is null****");
        }
        return isSave;
    }

    public boolean hasStr(String inputStr, String conditionStr){
        Pattern pattern = Pattern.compile(conditionStr);
        Matcher matcher = pattern.matcher(inputStr);
        return matcher.matches();
    }

    public List<String> hasStrArr(String inputStr, String conditionStr){
        Pattern pattern = Pattern.compile(conditionStr);
        Matcher matcher = pattern.matcher(inputStr);
        List<String> mData = new ArrayList<>();
        while (matcher.find()){
            mData.add(matcher.group());
        }
        return mData;
    }
}
